﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Media.Imaging;
using System.Runtime.Serialization.Json;
using Path = System.IO.Path;

namespace Gallery {
    public partial class Page : UserControl {
        /// <summary>
        /// 
        /// </summary>
        private const string baseSite = "http://artistsanon.com.au/"; // "http://www.somesite.com/Gallery.Web/";  //"http://artistsanon.com.au/images/";
        private string imagesUri = Path.Combine(baseSite, "images/");  //"http://artistsanon.com.au/images/";
        private string getfilesapi = Path.Combine(baseSite, "fileList.php"); // "http://artistsanon.com.au/fileList.php";
        public Page() {
            InitializeComponent();
            loadImages();
            imageHolder.MouseLeftButtonUp += new MouseButtonEventHandler(imageHolder_MouseLeftButtonUp);
        }

        void imageHolder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
            //  imageHolder.Children.Clear();
            msi.Source = null;
        }

        private void btnNext_Click(object sender, RoutedEventArgs e) {
            //btnNext.Content = "Pushed";
        }

        private void loadImages() {
            WebClient client = new WebClient();
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);

            client.OpenReadAsync(new Uri(getfilesapi));

        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
            if (e.Error != null || e.Cancelled) {
                if (e.Error != null && e.Error.Message != null) txtStatus.Text = e.Error.Message;
                return;
            }
            txtStatus.Text = string.Empty;

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Gallery));

            Gallery images = serializer.ReadObject(e.Result) as Gallery;


            if (images != null) {
                foreach (var image in images.images) {
                    if (image != null) {
                        Image img = new Image();
                        BitmapImage bmi = new BitmapImage(new Uri(imagesUri + image.filename, UriKind.Absolute));
                        img.Source = bmi;
                        img.Width = 150;
                        Button btn = new Button();
                        //btn.Width = 150;

                        btn.Content = img;
                        btn.Click += new RoutedEventHandler(btn_Click);
                        wrapPanel.Children.Add(btn);
                    }

                }
            }

        }

        void btn_Click(object sender, RoutedEventArgs e) {
            Button btn = e.OriginalSource as Button;
            if (btn != null) {
                Image img = btn.Content as Image;
                if (img != null) {
                    BitmapImage bitmapImage = img.Source as BitmapImage;
                    if (bitmapImage != null) {
                        //MessageBox.Show(bitmapImage.UriSource.AbsolutePath.ToString());
                        Image showImage = new Image();
                        BitmapImage showBm = new BitmapImage(new Uri(bitmapImage.UriSource.AbsoluteUri.Replace("thumbnails/",""), UriKind.Absolute));
                        showImage.Source = showBm;
                        //imageHolder.Children.Clear();
                        //imageHolder.Children.Add(showImage);

                        string newTilePath = bitmapImage.UriSource.LocalPath;
                        string[] splitPath = newTilePath.Split('/');

                        if (splitPath.Length > 0) {
                            string tilepath = "/GeneratedImages/setone/dzc_output_images/" + splitPath[splitPath.Length - 1].ToUpper().Replace(".JPG", ".xml");
                            msi.Source = new DeepZoomImageTileSource(new System.Uri(tilepath, UriKind.Relative));
                        }

                    }
                }
            }
        }

    }

    public static class MessageBox {
        public static void Show(string message) {
            HtmlPage.Window.Invoke("alert", message);
        }
    }

}

